package com.steven.concurrency.example.jihetongbu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.*;

public class CopyOnWriteListExam {

    /**
     *ConcurrentHashMap
     * ConcurrentSkipListMap都是线程安全的
     */
    private static List<Integer> list = new CopyOnWriteArrayList<>();
    private static Set<Integer> set = new CopyOnWriteArraySet<>();
    private static Set<Integer> set1 = new ConcurrentSkipListSet<>();
    //请求总数
    private static int clientTotal=5000;
    //并发数量
    private static int threadTotal=200;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Semaphore semaphore = new Semaphore(threadTotal);
        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
        for (int i = 0; i <clientTotal ; i++) {
            final int cop=i;
            executorService.execute(()->{
                try {
                    semaphore.acquire();
                    update(cop);
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        System.out.println(list.size());
    }

    private static void update(int cop) {

        list.add(cop);
    }




}
